<template>
  <div>
    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column prop="title" label="封面图" width="80">
        <template slot-scope="{ row }">
          <img
            class="cover-img"
            :src="row.fileId ? row.sysAttachment.httpUrl : defaultCourseImg"
            alt=""
          />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="课程标题" width="180">
        <template slot-scope="{ row }">
          <el-link @click="handleManage(row)" :underline="false">{{
            row.title
          }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="sourceUnit" label="建设单位">
        <template slot-scope="scope">
          <span>{{ showUnit(scope.row.tbSysDept) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="subhead" label="课程团队">
        <template slot-scope="scope">
          <span>{{
            scope.row.sysCourseTeamList.map((v) => v.userName).toString()
          }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="课程状态">
        <template slot-scope="scope">
          <span v-show="scope.row.status == 0">未发布</span>
          <span class="color-success" v-show="scope.row.status == 1"
            >已发布</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="auditStatus" label="审核状态">
        <template slot-scope="scope">
          <span v-show="scope.row.auditStatus == 1">待审核</span>
          <span class="color-success" v-show="scope.row.auditStatus == 2"
            >审核通过</span
          >
          <span class="color-danger" v-show="scope.row.auditStatus == 3"
            >不通过</span
          >
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column label="操作" width="220">
        <template slot-scope="{ row }">
          <el-button-group>
            <el-button type="primary" size="mini" @click="handleManage(row)">
              管理
            </el-button>
            <el-button
              v-if="row.status == 0"
              type="primary"
              size="mini"
              @click="handlePublish(row)"
            >
              发布
            </el-button>
            <el-button
              v-if="row.status == 1"
              type="warning"
              size="mini"
              @click="handleUnPublish(row)"
            >
              取消发布
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
      <div slot="empty" class="emptyBg">
        <el-empty :image-size="200"></el-empty>
      </div>
    </el-table>
    <Pagination
      v-show="parseInt(pageObj.count) > 0"
      :total="parseInt(pageObj.count)"
      @change="getList"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
    />
  </div>
</template>
<script>
import {
  sysCourseQuery,
  sysCourseDelete,
} from "@/api/admin/courseLibrary/sysCourse.js";
import Bus from "@/utils/bus.js";
import webinfo from "@/mixins/webinfo.js";
import { courseAudit } from "../../mixins/CourseAudit.js";
export default {
  mixins: [webinfo, courseAudit],
  data() {
    return {
      listQuery: {
        courseType: this.$route.meta.type,
        page: 1,
        limit: 10,
        sourceType: "1",
      },
      tableData: [],
      pageObj: {},
    };
  },
  props: ["sourceType"],
  computed: {
    showUnit: function () {
      return function (data) {
        if (data && data.childList) {
          return data.childList[0].name;
        } else {
          return "--";
        }
      };
    },
  },
  watch: {
    sourceType(v) {
      this.getList();
    },
  },
  mounted() {
    this.getList();
    Bus.$on("refreshCourseList", (v) => {
      if (v) {
        Object.assign(this.listQuery, v);
      }
      this.getList();
    });
  },
  beforeDestroy() {
    Bus.$off("refreshCourseList");
  },
  methods: {
    getList() {
      this.listQuery.sourceType = this.sourceType;
      if (this.listQuery.sourceType == 1) {
        this.listQuery.title = "";
      } else {
        this.listQuery.code = "";
      }
      sysCourseQuery(this.listQuery).then((res) => {
        this.tableData = res.data.data;
        this.pageObj = res.data;
      });
    },
    //管理
    handleManage(row) {
        let type = this.$route.meta.type;
        this.$store.commit("SETCURCOURSE", row);
        if (type == 1) {
            this.$router.push({ name: "course-setting" });
        }
        if (type == 5) {
            this.$router.push({ name: "micro-setting" });
        }
    
    },
    //编辑
    handleEdit(row) {
      Bus.$emit("showDialogCourseAdd", row);
    },
    //删除确认
    handleDelete(row) {
      this.$confirm("确定要删除吗?", "提示", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.delete(row);
        })
        .catch(() => {});
    },
    //删除
    delete(row) {
      sysCourseDelete(row.id).then((res) => {
        this.getList();
        this.$message({
          message: "操作成功",
          type: "success",
        });
      });
    },
  },
};
</script>
<style scoped>
.cover-img {
  display: block;
  width: 40px;
  height: 30px;
}
</style>